package com.library.controller;

import com.library.bean.LendExpand;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/*图书借还（管理员同意借书申请)   相关Controller层*/
@Controller
public class LendController {
    @Autowired
    private LendService lendService;

    /*管理员  同意借书申请*/
    @RequestMapping("/lendbookapply.html")
    public String bookLendApply(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long serNum = Long.parseLong(request.getParameter("serNum"));
        long bookId = Long.parseLong(request.getParameter("bookId"));
        if (lendService.lendBook(serNum,bookId)) {
            redirectAttributes.addFlashAttribute("succ", "管理员同意，读者借阅成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "图书借阅失败！");
        }
        String states = request.getParameter("states");
        if ("applying".equals(states))
            return "redirect:/applying.html";
        else
            return "redirect:/lendlist.html";
    }

    /*管理员  查看借还日志*/
    @RequestMapping("/lendlist.html")
    public ModelAndView lendList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        modelAndView.addObject("list", lendService.LendExpandList());
        return modelAndView;
    }

    /*管理员  查看借阅申请*/
    @RequestMapping("/applying.html")
    public ModelAndView applying(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        ArrayList<LendExpand> LendExpandList = lendService.LendExpandList();
        ArrayList<LendExpand> LendExpandListApplying = new ArrayList<>();
        for(int i = 0; i<LendExpandList.size(); i++){
            if (LendExpandList.get(i).getLendDate()==null&&LendExpandList.get(i).getBackDate()==null)
                LendExpandListApplying.add(LendExpandList.get(i));
        }
        if (LendExpandListApplying.size()>0){
            modelAndView.addObject("list", LendExpandListApplying);
            modelAndView.addObject("states","applying");
            return modelAndView;
        } else {
            return new ModelAndView("admin_lend_list", "succ", "没有借阅申请");
        }
    }

    /*管理员  查看未还书籍*/
    @RequestMapping("/noreturn.html")
    public ModelAndView noreturn(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        ArrayList<LendExpand> LendExpandList = lendService.LendExpandList();
        ArrayList<LendExpand> LendExpandListNoReturn = new ArrayList<>();
        for(int i = 0; i<LendExpandList.size(); i++){
            if (LendExpandList.get(i).getLendDate()!=null&&LendExpandList.get(i).getBackDate()==null)
                LendExpandListNoReturn.add(LendExpandList.get(i));
        }
        if (LendExpandListNoReturn.size()>0){
            modelAndView.addObject("list", LendExpandListNoReturn);
            modelAndView.addObject("states","noreturn");
            return modelAndView;
        } else {
            return new ModelAndView("admin_lend_list", "succ", "没有未还书籍");
        }
    }




    /*管理员 查看借还日志 依据具读者id查看借还日志*/
    @RequestMapping("/queryLendListByReaderId.html")
    public ModelAndView queryLendListByReaderId(long reader_id) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        ArrayList<LendExpand> myLendExpandList = lendService.myLendExpandList(reader_id);
        if(myLendExpandList.size()>0){
            modelAndView.addObject("list", myLendExpandList);
            return modelAndView;
        }else{
            return new ModelAndView("admin_lend_list", "succ", "没有对应读者的借阅记录");
        }
    }

    /*管理员 查看借阅申请 依据具读者id查看借还日志*/
    @RequestMapping("/queryLendListByReaderIdApplying.html")
    public ModelAndView queryLendListByReaderIdApplying(long reader_id) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        ArrayList<LendExpand> myLendExpandList = lendService.LendExpandListApplyingByReaderId(reader_id);
        if(myLendExpandList.size()>0){
            modelAndView.addObject("list", myLendExpandList);
            modelAndView.addObject("states","applying");
            return modelAndView;
        }else{
            ModelAndView modelAndView2 = new ModelAndView("admin_lend_list", "succ", "没有对应读者的申请借阅记录");
            modelAndView2.addObject("states","applying");
            return modelAndView2;
        }
    }

    /*管理员 查看未还图书 依据具读者id查看借还日志*/
    @RequestMapping("/queryLendListByReaderIdNoReturn.html")
    public ModelAndView queryLendListByReaderIdNoReturn(long reader_id) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        ArrayList<LendExpand> myLendExpandList = lendService.LendExpandListNoReturnByReaderId(reader_id);
        if(myLendExpandList.size()>0){
            modelAndView.addObject("list", myLendExpandList);
            modelAndView.addObject("states","noreturn");
            return modelAndView;
        }else{
            ModelAndView modelAndView2 = new ModelAndView("admin_lend_list", "succ", "没有对应读者的未还记录");
            modelAndView2.addObject("states","noreturn");
            return modelAndView2;
        }
    }


    /*管理员  删除借还日志*/
    @RequestMapping("/deletelend.html")
    public String deleteLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long serNum = Long.parseLong(request.getParameter("serNum"));
        if (lendService.deleteLend(serNum) > 0) {
            redirectAttributes.addFlashAttribute("succ", "记录删除成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "记录删除失败！");
        }
        String states = request.getParameter("states");
        if ("applying".equals(states))
            return "redirect:/applying.html";
        else
            return "redirect:/lendlist.html";
    }


    /*读者  查看我的借还*/
    @RequestMapping("/mylend.html")
    public ModelAndView myLend(HttpServletRequest request) {
        //session获取ReaderCard对象（包含读者账号 、读者密码、 读者名字) ----读者登录就会产生
        ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
        ModelAndView modelAndView = new ModelAndView("reader_lend_list");
        modelAndView.addObject("readerId", readerCard.getReaderId());
        modelAndView.addObject("list", lendService.myLendExpandList(readerCard.getReaderId()));
        return modelAndView;
    }

    /*读者    申请借阅读书 */
    @RequestMapping("/lendbook.html")
    public String bookLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long bookId = Long.parseLong(request.getParameter("bookId"));
        //获取读者id
        long readerId = ((ReaderCard) request.getSession().getAttribute("readercard")).getReaderId();
        if (lendService.lendBookApply(bookId, readerId)) {
            redirectAttributes.addFlashAttribute("succ", "图书借阅申请成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "图书借阅申请失败！");
        }
        return "redirect:/reader_books.html";
    }

    /*读者    归还读书*/
    @RequestMapping("/returnbook.html")
    public String bookReturn(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long bookId = Long.parseLong(request.getParameter("bookId"));
        long readerId = ((ReaderCard) request.getSession().getAttribute("readercard")).getReaderId();
        if (lendService.returnBook(bookId, readerId)) {
            redirectAttributes.addFlashAttribute("succ", "图书归还成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "图书归还失败！");
        }
        return "redirect:/reader_books.html";
    }
}
